home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_06_07 / v6n7078a.txt < prev    next >
Text File  |  1989-09-26  |  3KB  |  137 lines

  1. /* Set Device Driver Attributes */
  2. /* Philip J. Erdelsky - June 28, 1988 */
  3.  
  4. #include <fcntl.h>
  5. #include <stdio.h>
  6.  
  7. typedef unsigned char byte;
  8. typedef unsigned int word;
  9.  
  10. /* format of device header */
  11.  
  12. struct device_header {
  13.   word offset, segment;
  14.   word attributes;
  15.   word strategy_entry;
  16.   word interrupt_entry;
  17.   char name[8];
  18.   };
  19.  
  20. /* device header attributes */
  21.  
  22. #define CHAR_DEVICE_ATTRIBUTE     0x8000
  23. #define STI_DEVICE_ATTRIBUTE      0x0001
  24. #define STO_DEVICE_ATTRIBUTE      0x0002
  25. #define NUL_DEVICE_ATTRIBUTE      0x0004
  26. #define CLOCK_DEVICE_ATTRIBUTE    0x0008
  27. #define SPECIAL_ATTRIBUTE         0x0010
  28. #define IOCTL_ATTRIBUTE           0x4000
  29. #define NON_IBM_FORMAT_ATTRIBUTE  0x2000
  30.  
  31. static union {
  32.  
  33.   struct {
  34.     word signature;
  35.     word bytes_in_final_sector;
  36.     word size_of_file;
  37.     word number_of_relocation_items;
  38.     word size_of_header;
  39.     word paragraphs_after_end;
  40.     word high_low_loader_switch;
  41.     word SS;
  42.     } exe_header;
  43.  
  44.   struct device_header device_header;
  45.  
  46.   char unstructured[512];
  47.  
  48.   } buffer;
  49.  
  50. #define PARAGRAPH 16
  51.  
  52. static int capital(c) int c; {
  53. return ('a'<=c && c<='z' ? c-('a'-'A') : c);
  54. }
  55.  
  56. void main(argc, argv) int argc; char **argv; {
  57. int n, exe_file, sys_file;
  58.  
  59. if (argc<5) {
  60.   fputs("Missing argument(s)", stderr);
  61.   exit(1);
  62.   }
  63.  
  64. if ((exe_file = open(argv[1],O_RDONLY|O_RAW,0))<0) {
  65.   fputs("Can't open ", stderr);
  66.   fputs(argv[1], stderr);
  67.   exit(1);
  68.   }
  69.  
  70. if ((sys_file = open(argv[2],O_WRONLY|O_CREAT|O_TRUNC|O_RAW,0))<0) {
  71.   fputs("Can't open ", stderr);
  72.   fputs(argv[2], stderr);
  73.   exit(1);
  74.   }
  75.  
  76. if (read(exe_file, buffer.unstructured, PARAGRAPH) < PARAGRAPH) {
  77.   fputs(".EXE file format error", stderr);
  78.   exit(1);
  79.   }
  80.  
  81. if (buffer.exe_header.number_of_relocation_items!=0) {
  82.   fputs(".EXE file contains relocation items", stderr);
  83.   exit(1);
  84.   }
  85.  
  86.   {
  87.   int i;
  88.   for (i=buffer.exe_header.size_of_header; i>1; i--)
  89.     if (read(exe_file, buffer.unstructured, PARAGRAPH) < PARAGRAPH) {
  90.       fputs(".EXE file format error", stderr);
  91.       exit(1);
  92.       }
  93.   }
  94.  
  95. n = read(exe_file, buffer.unstructured, sizeof(buffer));
  96.  
  97. if (n<sizeof(buffer.device_header)) {
  98.   fputs(".EXE file format error", stderr);
  99.   exit(1);
  100.   }
  101.  
  102.   {
  103.   char *p;
  104.   for (p=argv[3]; *p!=0; p++) switch (capital(*p)) {
  105.     case 'C': buffer.device_header.attributes |= CHAR_DEVICE_ATTRIBUTE; break;
  106.     case 'I': buffer.device_header.attributes |= STI_DEVICE_ATTRIBUTE; break;
  107.     case 'O': buffer.device_header.attributes |= STO_DEVICE_ATTRIBUTE; break;
  108.     case 'N': buffer.device_header.attributes |= NUL_DEVICE_ATTRIBUTE; break;
  109.     case 'T': buffer.device_header.attributes |= CLOCK_DEVICE_ATTRIBUTE; break;
  110.     case 'S': buffer.device_header.attributes |= SPECIAL_ATTRIBUTE; break;
  111.     case 'L': buffer.device_header.attributes |= IOCTL_ATTRIBUTE; break;
  112.     case 'X': buffer.device_header.attributes |= NON_IBM_FORMAT_ATTRIBUTE; break;
  113.     }
  114.   }
  115.  
  116. if (buffer.device_header.attributes&CHAR_DEVICE_ATTRIBUTE) {
  117.   int c, i;
  118.   for (i=0; (c=argv[4][i])!=0 && i<8; i++)
  119.     buffer.device_header.name[i] = capital(c);
  120.   }
  121. else {
  122.   char *p;
  123.   int u = 0;
  124.   for (p=argv[4]; *p!=0; p++) u = 10*u+(*p)-'0';
  125.   buffer.device_header.name[0] = u;
  126.   }
  127.  
  128. do {
  129.   if (write(sys_file, buffer.unstructured, n) < n) {
  130.     fputs("File write error", stderr);
  131.     exit(1);
  132.     }
  133.   n = read(exe_file, buffer.unstructured, sizeof(buffer));
  134.   } while (n>0);
  135.  
  136. }
  137.